package com.csw.user;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class Demo1login {
    public static void main(String[] args) throws Exception {

        //1|用户输入用户名密码

        Scanner scanner = new Scanner(System.in);


        //用户输入用户名

        System.out.println("请输入用户名：");
        String username = scanner.next();


        //输入密码
        System.out.println("请输入密码：");
        String password = scanner.next();


        //2、验证用户名是否存在
        Class.forName("com.mysql.jdbc.Driver");

        Connection con = DriverManager.getConnection("jdbc:mysql://master:3306/user", "root", "123456");

        PreparedStatement stat = con.prepareStatement("select * from t_user where username=?");

        stat.setString(1, username);

        ResultSet resultSet = stat.executeQuery();

        if (!resultSet.next()) {
            System.out.println("用户名不存在。。。");
            return;
        }

        //3、判断密码是否正确

        String password1 = resultSet.getString("password");

        if (!password.equals(password1)) {
            System.out.println("密码不对");
            return;
        }

        System.out.println("登录成功");


        con.close();
    }
}
